package xyz.tanxiao.links.core.mapping;

import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 主键ID转Base62的长链接压缩规则 (62^7)
 *
 * @author 万成波
 * @email 2300064869@qq.com
 * @createTime 2023-06-10 15:18:03 星期六
 */
@Deprecated
@Component
public class PrimaryKeyToBase62MappingKey implements LongLinkMappingKey<Long> {

	private static final String BASE62_STRING = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	private static final char[] BASE62_CHARACTERS = BASE62_STRING.toCharArray();

	private static final int BASE62_LENGTH = BASE62_CHARACTERS.length;


	/**
	 * 长链接映射Key生成方法
	 *
	 * @param primaryKey 数据库主键 (数字类型)
	 * @return 长链接映射Key
	 */
	@Override
	public List<String> toMappingKey(Long primaryKey) {
		StringBuilder mappingKey = new StringBuilder();
		while (primaryKey > 0) {
			mappingKey.insert(0, BASE62_CHARACTERS[(int) (primaryKey % BASE62_LENGTH)]);
			primaryKey /= BASE62_LENGTH;
		}
		return Collections.singletonList(mappingKey.toString());
	}

}